<template>
<!-- template向外暴露只能暴露一个根标签 -->
    <div>
        <home-header></home-header> 
        <home-swiper :list='swiperList'></home-swiper>
        <home-icons :list="iconList"></home-icons>
        <home-recommend :list="recommendList"></home-recommend>
        <home-weekend :list='weekendList'></home-weekend>
    </div>
</template>

<script>
import HomeHeader from './components/Header'
import HomeSwiper from './components/swiper'
import HomeIcons from './components/Icons'
import HomeRecommend from './components/Recommend'
import HomeWeekend from './components/Weekend'
import axios from 'axios'
import { mapState } from 'vuex'
export default {
    name: 'Home',
    components: {
        HomeHeader,
        HomeSwiper,
        HomeIcons,
        HomeRecommend,
        HomeWeekend,
    },
    data () {
        return {
            swiperList:[],  
            iconList: [],
            recommendList: [],
            weekendList: [],
            lastCtiy: ''
        }
    },
    computed: {
        ...mapState(['city'])
    },
    methods: {
        getHomeInfo () {
            axios.get('./api/index.json?city=' + this.city)
                .then(this.getHomeInfoSucc)
        },
        getHomeInfoSucc(res) {
            res = res.data
            // res正常返回了结果（ret判断）,且里面有data内容
            if(res.ret && res.data) {
                const data = res.data

                this.swiperList = data.swiperList
                this.iconList = data.iconList
                this.recommendList =  data.recommendList
                this.weekendList = data.weekendList
                // console.log(this.recommendList)
            }
        }
    },
    mounted () {
        this.lastCtiy = this.city
        this.getHomeInfo()
    },
    // 使用keepalive之后，每次回到页面都会到activated的生命周期函数--页面重新显示时激活
    activated () {
        if (this.lastCtiy !== this.city) {
            // 页面不相同，重新发送一次ajax请求
            this.lastCtiy = this.city
            this.getHomeInfo()
        }

    }
}
</script>

<style>
    .home{
        font-size: 30px;
    }
</style>